package com.webbei.share.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.webbei.share.service.ShareSer;
import com.webbei.pojo.Customs;
public class ShareCommentUpload extends ActionSupport{
	private ShareSer ser;
	private Long customId;
	private String filepath;
	private static final int BUFFER_SIZE=16*1024;
	public String execute(){
		String[] filearr=filepath.split(";");
		List<Customs> list=ser.getCustomById(customId);
		String username=list.get(0).getUserName();
		String savePath=ServletActionContext.getServletContext().getRealPath("");
		String insertpath="";
		String filename="";
		String tempPath=savePath+"\\img\\temp\\";
		savePath+="\\img\\resources\\"+username+"\\"+customId+"\\";
		File temp=new File(savePath);
		if(!temp.exists())
			temp.mkdirs();
		File source;
		File des;
		for(int i=0;i<filearr.length;i++)
		{
			filename=filearr[i].substring(filearr[i].lastIndexOf("\\")+1);
			source=new File(tempPath+filename);
			des=new File(savePath+filename);
			try {
				Copy(source, des);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			insertpath+="img/resources/"+username+"/"+customId+"/"+filename+";";
			source.delete();
		}
		//System.out.println(insertpath);
		insertpath+=list.get(0).getCustomUploadFile();
		Customs tempcus=new Customs();
		tempcus.setCustomId(customId);
		tempcus.setCustomUploadFile(insertpath);
		System.out.println(insertpath);
		if(ser.SetCustonUploadFile(tempcus)>0)
		{
			try {
				ServletActionContext.getResponse().getWriter().write("success");
				ServletActionContext.getResponse().setContentType("text/html");
				return null;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}
	private static void Copy(File src,File des) throws FileNotFoundException{
    	InputStream in=null;
    	OutputStream out=null;
    	try{
    		in=new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
    		out=new BufferedOutputStream(new FileOutputStream(des), BUFFER_SIZE);
    		byte []buffer=new byte[BUFFER_SIZE];
    		int len=0;
    		while((len=in.read(buffer))>0){
    			out.write(buffer, 0, len);
    		}
    	}
    	catch (IOException e){
    		e.printStackTrace();
    		return;
    	}finally{
    		try{
    			if(in!=null)
    				in.close();
    			if(out!=null)
    				out.close();
    		}catch (IOException e)
    		{
    			e.printStackTrace();
    		}
    	}
    }
	public ShareSer getSer() {
		return ser;
	}
	public void setSer(ShareSer ser) {
		this.ser = ser;
	}
	public Long getCustomId() {
		return customId;
	}
	public void setCustomId(Long customId) {
		this.customId = customId;
	}
	public String getFilepath() {
		return filepath;
	}
	public void setFilepath(String filepath) {
		this.filepath = filepath;
	}
	
}
